home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 15 / PERSIST2.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  4KB  |  143 lines

  1. // File from page 667 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: PERSIST2.CPP -- Improved MI persistence
  34. #include <fstream.h>
  35. #include <string.h>
  36.  
  37. class persistent {
  38. public:
  39.   virtual void write(ostream& out) const = 0;
  40.   virtual void read(istream& in) = 0;
  41. };
  42.  
  43. class data {
  44. protected:
  45.   float f[3];
  46. public:
  47.   data(float f0 = 0.0, float f1 = 0.0,
  48.     float f2 = 0.0) {
  49.     f[0] = f0;
  50.     f[1] = f1;
  51.     f[2] = f2;
  52.   }
  53.   void print(const char* msg = "") const {
  54.     if(*msg) cout << msg << endl;
  55.     for(int i = 0; i < 3; i++)
  56.       cout << "f[" << i << "] = "
  57.            << f[i] << endl;
  58.   }
  59. };
  60.  
  61. class wdata1 : public persistent, public data {
  62. public:
  63.   wdata1(float f0 = 0.0, float f1 = 0.0,
  64.     float f2 = 0.0) : data(f0, f1, f2) {}
  65.   void write(ostream& out) const {
  66.     out << f[0] << " " << f[1] << " " << f[2];
  67.   }
  68.   void read(istream& in) {
  69.     in >> f[0] >> f[1] >> f[2];
  70.   }
  71. };
  72.  
  73. class wdata2 : public data, public persistent {
  74. public:
  75.   wdata2(float f0 = 0.0, float f1 = 0.0,
  76.     float f2 = 0.0) : data(f0, f1, f2) {}
  77.   void write(ostream& out) const {
  78.     out << f[0] << " " << f[1] << " " << f[2];
  79.   }
  80.   void read(istream& in) {
  81.     in >> f[0] >> f[1] >> f[2];
  82.   }
  83. };
  84.  
  85. class conglomerate : public data,
  86. public persistent {
  87.   char* name; // Contains a pointer
  88.   wdata1 d1;
  89.   wdata2 d2;
  90. public:
  91.   conglomerate(const char* nm = "",
  92.     float f0 = 0.0, float f1 = 0.0,
  93.     float f2 = 0.0, float f3 = 0.0,
  94.     float f4 = 0.0, float f5 = 0.0,
  95.     float f6 = 0.0, float f7 = 0.0,
  96.     float f8= 0.0) : data(f0, f1, f2),
  97.     d1(f3, f4, f5), d2(f6, f7, f8) {
  98.     name = new char[strlen(nm) + 1];
  99.     strcpy(name, nm);
  100.   }
  101.   void write(ostream& out) const {
  102.     int i = strlen(name) + 1;
  103.     out << i << " "; // Store size of string
  104.     out << name << endl;
  105.     d1.write(out);
  106.     d2.write(out);
  107.     out << f[0] << " " << f[1] << " " << f[2];
  108.   }
  109.   // Must read in reverse order as write:
  110.   void read(istream& in) {
  111.     delete []name; // Remove old storage
  112.     int i;
  113.     in >> i >> ws; // Get int, strip whitespace
  114.     name = new char[i];
  115.     in.getline(name, i);
  116.     d1.read(in);
  117.     d2.read(in);
  118.     in >> f[0] >> f[1] >> f[2];
  119.   }
  120.   void print() const {
  121.     data::print(name);
  122.     d1.print();
  123.     d2.print();
  124.   }
  125. };
  126.  
  127. main() {
  128.   {
  129.     ofstream data("data.dat");
  130.     conglomerate C("This is conglomerate C",
  131.       1.1, 2.2, 3.3, 4.4, 5.5,
  132.       6.6, 7.7, 8.8, 9.9);
  133.     cout << "C before storage" << endl;
  134.     C.print();
  135.     C.write(data);
  136.   } // Closes file
  137.   ifstream data("data.dat");
  138.   conglomerate C;
  139.   C.read(data);
  140.   cout << "after storage: " << endl;
  141.   C.print();
  142. }
  143.